package com.androidian.tippit.fragments;

import com.androidian.tippit.ActivityHome;
import com.androidian.tippit.R;
import com.androidian.tippit.utilities.Fonts;
import com.androidian.tippit.utilities.Callbacks.WizardLocker;
import com.viewpagerindicator.LinePageIndicator;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FragmentTotals extends Fragment
{

	private WizardLocker mWizardLocker;
	
	private View mCard;
	private LinearLayout mContainer;
	
	private Animation animation;

	private ActivityHome mActivity;
		
	
	
	@Override
	public void onAttach(Activity activity) 
	{
		super.onAttach(activity);
		
		mWizardLocker = (WizardLocker) activity;
		
		mActivity = (ActivityHome) activity;
		 		
	}


	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		animation = AnimationUtils.loadAnimation(getActivity(),
				R.anim.card_animation);
		
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
	{
		
		final View view = inflater.inflate(R.layout.fragment_totals,
				container, false);		
		
		Fonts.setRobotoThinFont(getActivity(), view);
		
		mContainer = (LinearLayout)
				view.findViewById(R.id.container);
		
		mCard = inflater
				.inflate(R.layout.view_simpletotal, mContainer, false);
		

		return view;
		
	}
	
	
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) 
	{
		super.setUserVisibleHint(isVisibleToUser);

		if (isVisibleToUser) 
		{
			
			mWizardLocker.lockNavigation(false, true, false);
						
			
			if(mCard != null)
			{
				
				final TextView billTotalTextView = (TextView)
						mCard.findViewById(R.id.billTotalTextView);
				billTotalTextView.setText("$" + String.format("%.02f", mActivity.billTotal));
				
				final TextView peopleNumberTextView = (TextView)
						mCard.findViewById(R.id.peopleNumberTextView);
				peopleNumberTextView.setText(String.valueOf(mActivity.peopleNumber));
				
				final TextView tipPercentageTextView = (TextView)
						mCard.findViewById(R.id.tipPercentageTextView);
				tipPercentageTextView.setText(String.valueOf(mActivity.tipPercentage) + "%");
				
				final TextView billWithTipTextView = (TextView)
						mCard.findViewById(R.id.billWithTipTextView);
				billWithTipTextView.setText("$" + String.format("%.02f", mActivity.billWithTipTotal));
				
				final TextView billPerPersonTextView = (TextView)
						mCard.findViewById(R.id.billPerPersonTextView);
				billPerPersonTextView.setText("$" + String.format("%.02f", mActivity.billPerPerson));
				
				final TextView totalTipTextView = (TextView)
						mCard.findViewById(R.id.totalTipTextView);
				totalTipTextView.setText("$" + String.format("%.02f", mActivity.tipTotal));
				
				
				if(!mCard.isShown())
				{
					
					mContainer.addView(mCard);
										
					mCard.startAnimation(animation);  
					
					animation.setAnimationListener(new AnimationListener()
					{

						@Override
						public void onAnimationEnd(Animation animation)
						{
							
							final View mActivityRoot = (View)
									mActivity.findViewById(R.id.root);

							mActivityRoot.invalidate();

						}

						@Override
						public void onAnimationRepeat(Animation animation) 
						{

							// Not used
							
						}

						@Override
						public void onAnimationStart(Animation animation) 
						{
							
							// Not used
							
						}						
						
					});
					
				}
					
			}
			
		}

	}
	
	
	
	

}
